using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BANSACH.BusinessLayer.DataLayer;
using BANSACH.BusinessLayer;
using System.Data.SqlClient;


namespace BANSACH_GUI
{
    public partial class frmTblSACHDetail_Cb : FormBase
    {
        public EventHandler OnAddOrEditEntity;
        private bool _isNew;
        private TblSACHFactory objDao;
        private TblSACH _Entity;
        TblTHELOAIFactory tloaiDao;
        TblNXBFactory nxbDao;
        //
        DataView dtnxb, dttheloai;
        public TblSACH Entity
        {
            get { return _Entity; }
            set { _Entity = value; }
        }
        public frmTblSACHDetail_Cb()
        {
            InitializeComponent();
            _isNew = true;
            btnSave.Text = "Lưu && thêm mới";
            _Entity = null;
        }
        public frmTblSACHDetail_Cb(TblSACH EntityEdit)
        {
            InitializeComponent();
            _isNew = false;
            btnSave.Text = "Lưu";
            _Entity = EntityEdit;           
        }
        private void frmTblSACHDetail_Load(object sender, EventArgs e)
        {
            objDao = new TblSACHFactory();
            // Load thể loại
            tloaiDao = new TblTHELOAIFactory();
            DataTable data = tloaiDao.GetAll();
            dttheloai = data.DefaultView;
            dttheloai.Sort = "MaTL"; // Sắp xếp theo mã để tìm kiếm
            cb_theloai.DataSource = dttheloai;
            cb_theloai.DisplayMember = "TenTL";
            cb_theloai.ValueMember = "MaTL";            
            // Load nhà xuất bản
            nxbDao = new TblNXBFactory();
            data = nxbDao.GetAll();
            dtnxb = data.DefaultView;
            dtnxb.Sort = "MaNXB"; // Sắp xếp theo mã để tìm kiếm
            cb_nxb.DataSource = dtnxb;
            cb_nxb.DisplayMember = "TenNXB";
            cb_nxb.ValueMember = "MaNXB";
            // Hiển thị thực thể
            if (_Entity != null)
                ViewEntity();
            else
                _Entity = new TblSACH();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            SetEntityValue();
            if (_isNew)
            {
                if (objDao.Insert(_Entity))
                {
                    if (OnAddOrEditEntity != null) OnAddOrEditEntity(_Entity, e);
                    NewItem();
                }
            }
            else
            {
                if (objDao.Update(_Entity))
                {
                    if (OnAddOrEditEntity != null) OnAddOrEditEntity(_Entity, e);
                    this.Close();
                }
            }
        }
        private void NewItem()
        { 
            
				 txt_MaSach.Text ="";
				 txt_TenSach.Text ="";
				 txt_TacGia.Text ="";
                 cb_nxb.SelectedIndex = -1;
				 txt_NamXB.Text ="";
                 cb_theloai.SelectedIndex = -1;
				 txt_TomTat.Text ="";
				 txt_UrlAnh.Text ="";
				 txt_GiaBia.Text ="";
				 txt_GiaBan.Text ="";
				 txt_SoluongKK.Text ="";
				 txt_Soluong.Text ="";
        }
        private void ViewEntity()
        {           
				 txt_MaSach.Text = _Entity.MaSach;
				 txt_TenSach.Text = _Entity.TenSach;
				 txt_TacGia.Text = _Entity.TacGia;
                 int id;
                 id = dtnxb.Find(_Entity.MaNXB);
                 cb_nxb.SelectedIndex = id;
				 txt_NamXB.Text = _Entity.NamXB;
                 id = dttheloai.Find(_Entity.MaTL);
                 cb_theloai.SelectedIndex = id;				 
				 txt_TomTat.Text = _Entity.TomTat;
				 txt_UrlAnh.Text = _Entity.UrlAnh;
                 pb_Anh.Image = CreateImage(txt_UrlAnh.Text);
				 txt_GiaBia.Text = _Entity.GiaBia.ToString();
				 txt_GiaBan.Text = _Entity.GiaBan.ToString();
				 date_NgayKK.Value = _Entity.NgayKK.Value;
				 txt_SoluongKK.Text = _Entity.SoluongKK.ToString();
				 txt_Soluong.Text = _Entity.Soluong.ToString();
        }
        private void SetEntityValue()
        {          
				 _Entity.MaSach=txt_MaSach.Text;
				 _Entity.TenSach=txt_TenSach.Text;
				 _Entity.TacGia=txt_TacGia.Text;
				 _Entity.MaNXB= cb_nxb.SelectedValue.ToString();
                 _Entity.TenNXB = cb_nxb.SelectedItem.ToString();
				 _Entity.NamXB=txt_NamXB.Text;
				 _Entity.MaTL= cb_theloai.SelectedValue.ToString();
                 _Entity.TenTL = cb_theloai.Text.ToString();
				 _Entity.TomTat=txt_TomTat.Text;
				 _Entity.UrlAnh=txt_UrlAnh.Text;
				 _Entity.GiaBia=txt_GiaBia.LongValue;
				 _Entity.GiaBan=txt_GiaBan.LongValue;
				 _Entity.NgayKK=date_NgayKK.Value;
				 _Entity.SoluongKK=txt_SoluongKK.IntValue;
				 _Entity.Soluong=txt_Soluong.IntValue;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnchonAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Bitmap files(*.bmp)|*.bmp|JPEG|*.jpg;*.jpeg;*.jpe|All files|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txt_UrlAnh.Text = ofd.FileName;
                pb_Anh.Image = CreateImage(txt_UrlAnh.Text);
            }
        }
        public Image CreateImage(string path)
        {
            try
            {
                Image img = Image.FromFile(path);
                return img;
            }
            catch
            { 
                return null; 
            }
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            //1. lay du lieu
            DataLayerBase objDao = new DataLayerBase();
           // SqlParameter masach = new SqlParameter("@masach", txt_MaSach.Text);
            DataTable data = objDao.Do_SpSelect("sp_thongkedausach");

            //2.khai bao report va dua dl len report
            Reports.rptThongKeSach report = new Reports.rptThongKeSach();

            // truyen tham so
            report.DataDefinition.FormulaFields["ngaythang"].Text = "'" + FrameWorkSystem.CommonFunction.DateTime2LongString(date_NgayKK.Value) + "'";
            report.DataDefinition.FormulaFields["tieude"].Text = "' THỐNG KÊ SÁCH'";
            report.SetDataSource(data);

            //3. hien thi form
            Reports.frmPreview preview = new Reports.frmPreview();
            preview.Report = report;
            preview.Show();
        }
       
    }
}
